using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace StartMenuSearcher
{
	public partial class frmSearch : Form
	{
		StartMenuReader reader = new StartMenuReader();
		//StartMenuConfig config = new StartMenuConfig();

		public frmSearch()
		{
			InitializeComponent();
		}

		private void frmSearch_Load(object sender, EventArgs e)
		{
			// ctrl + alt + backslash
			GlobalHotkeys.Register(Keys.OemPipe, KeyModifiers.Alt | KeyModifiers.Ctrl);
			GlobalHotkeys.KeyPressed += new EventHandler(ShowHide);

			RefreshShortcuts();
		}

		public bool ShowIfHidden()
		{
			if (!this.Visible || !this.ContainsFocus)
			{
				this.Show();
				this.Activate();
				this.WindowState = FormWindowState.Normal;
				this.txtSearch.SelectAll();
				this.txtSearch.Focus();
				return true;
			}
			else
			{
				return false;
			}
		}

		private void ShowHide(object sender, EventArgs args)
		{
			if(!ShowIfHidden())
			{
				this.Hide();
			}
		}

		private void txtSearch_TextChanged(object sender, EventArgs e)
		{
			RefreshShortcuts();
		}

		private void RefreshShortcuts()
		{
			lstShortcuts.BeginUpdate();

			lstShortcuts.Items.Clear();
			lstShortcuts.Items.AddRange(reader.GetListBoxItems(txtSearch.Text));

			if (lstShortcuts.Items.Count > 0)
			{
				lstShortcuts.SelectedIndex = 0;
			}

			lstShortcuts.EndUpdate();
		}

		private void btnRun_Click(object sender, EventArgs e)
		{
			Shortcut selected = (Shortcut)lstShortcuts.SelectedItem;
			if (selected == null && lstShortcuts.Items.Count > 0)
			{
				selected = (Shortcut)lstShortcuts.Items[0];
			}

			if (selected != null)
			{
				selected.Run();
				txtSearch.Text = string.Empty;
				this.Hide();
			}
		}

		private void frmSearch_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				e.SuppressKeyPress = true;
				ShowHide(sender, e);
			}
			else if ((e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) && !lstShortcuts.Focused)
			{
				e.SuppressKeyPress = true;

				lstShortcuts.Focus();

				if (e.KeyCode == Keys.Up && lstShortcuts.SelectedIndex > 0)
				{
					lstShortcuts.SelectedIndex--;
				}

				if (e.KeyCode == Keys.Down && lstShortcuts.SelectedIndex < lstShortcuts.Items.Count - 1)
				{
					lstShortcuts.SelectedIndex++;
				}
			}
			else if (e.KeyCode == Keys.Delete && !txtSearch.Focused)
			{
				txtSearch.Focus();
				if (txtSearch.SelectedText.Length == 0)
				{
					if (txtSearch.SelectionStart != txtSearch.Text.Length)
					{
						int oldStart = txtSearch.SelectionStart;
						txtSearch.Text = txtSearch.Text.Substring(0, txtSearch.SelectionStart)
													 + txtSearch.Text.Substring(Math.Min(txtSearch.SelectionStart + 2, txtSearch.Text.Length));
						txtSearch.SelectionStart = oldStart;
					}
				}
				else
				{
					txtSearch.SelectedText = string.Empty;
				}
			}
		}

		private void frmSearch_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!txtSearch.Focused)
			{
				txtSearch.Focus();
				if (!char.IsControl(e.KeyChar))
				{
					txtSearch.SelectedText = e.KeyChar.ToString();
				}
				else if (e.KeyChar == '\b')
				{
					if (txtSearch.SelectedText.Length == 0)
					{
						if (txtSearch.SelectionStart != 0)
						{
							int oldStart = txtSearch.SelectionStart;
							txtSearch.Text = txtSearch.Text.Substring(0, txtSearch.SelectionStart - 1)
														 + txtSearch.Text.Substring(Math.Min(txtSearch.SelectionStart + 1, txtSearch.Text.Length));
							txtSearch.SelectionStart = oldStart - 1;
						}
					}
					else
					{
						txtSearch.SelectedText = string.Empty;
					}
				}
			}
		}

		private void sysTrayExit_Click(object sender, EventArgs e)
		{
			Close();
			Application.Exit();
		}

		private void frmSearch_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason == CloseReason.UserClosing)
			{
				e.Cancel = true;
				if (Visible)
					ShowHide(sender, e);
			}
			else
			{
				sysTrayContextMenu.Dispose();
			}
		}

		private void frmSearch_Resize(object sender, EventArgs e)
		{
			if (WindowState == FormWindowState.Minimized && Visible)
			{
				Hide();
			}
			else if (WindowState == FormWindowState.Normal && !Visible)
			{
				ShowHide(sender, e);
			}
		}
	}
}